package demo.bocweb.login_module.activity;

import android.os.CountDownTimer;
import android.text.InputType;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;

import butterknife.BindView;
import demo.bocweb.com.sdk.baseApi.RequestHelper;
import demo.bocweb.com.sdk.config.ARoutConfig;
import demo.bocweb.com.sdk.config.AppConfig;
import demo.bocweb.com.sdk.manager.MessageEvent;
import demo.bocweb.com.sdk.util.Loading;
import demo.bocweb.com.sdk.util.MD5Utils;
import demo.bocweb.com.sdk.util.SP;
import demo.bocweb.com.sdk.util.StringUtils;
import demo.bocweb.com.sdk.util.ToastyUtil;
import demo.bocweb.login_module.base.BaseLoginActivity;
import demo.bocweb.login_module.bean.LoginBean;
import demo.bocweb.user_module.R;
import demo.bocweb.user_module.R2;
import okhttp3.RequestBody;

/**
 * 登录
 * Created by zhelun on 2018/1/26.
 */
@Route(path = ARoutConfig.loginPath)
public class LoginActivity extends BaseLoginActivity {
    @BindView(R2.id.et_phone)
    EditText etPhone;
    @BindView(R2.id.et_pwd)
    EditText etPwd;
    @BindView(R2.id.iv_close)
    ImageView ivClose;
    @BindView(R2.id.logo)
    ImageView logo;
    @BindView(R2.id.eye)
    ImageView eye;
    @BindView(R2.id.tv_login)
    TextView tvLogin;
    @BindView(R2.id.tv_findPsw)
    TextView tvFindPsw;
    @BindView(R2.id.tv_registe)
    TextView tvRegiste;
    private CountDownTimer timer;
    private boolean isLoginOut = false;

    @Override
    public int getLayoutId() {
        return R.layout.activity_login;
    }

    @Override
    public void onBackPressed() {
        if (AppConfig.isMustLogin) {
            finish();
        } else {
            if (isLoginOut) {
                ARouter.getInstance().build(ARoutConfig.homeActivityPath).greenChannel().navigation();
            }
            finish();
        }
    }

    @Override
    protected void initData() {
        super.initData();
//        btnCode.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                sendSmsCode();
//            }
//        });
        isLoginOut = getIntent().getBooleanExtra("isLoginOut", false);
        ivClose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });

        tvLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                login();
            }
        });
        eye.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int inputType = etPwd.getInputType();
                if (inputType == (InputType.TYPE_CLASS_TEXT | EditorInfo.TYPE_TEXT_VARIATION_PASSWORD)) {
                    eye.setImageResource(R.mipmap.ic_login_eye_open);
                    etPwd.setInputType(InputType.TYPE_CLASS_TEXT | EditorInfo.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
                } else {
                    eye.setImageResource(R.mipmap.ic_login_eye_close);
                    etPwd.setInputType(InputType.TYPE_CLASS_TEXT | EditorInfo.TYPE_TEXT_VARIATION_PASSWORD);
                }
                etPwd.setSelection(etPwd.getText().toString().length());
            }
        });
        tvFindPsw.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ARouter.getInstance().build(ARoutConfig.findPwdPath).greenChannel().navigation();
            }
        });
        tvRegiste.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ARouter.getInstance().build(ARoutConfig.registerPath).greenChannel().navigation();
            }
        });

    }

    @Override
    public void loginSuccess(LoginBean bean) {
//        ARouter.getInstance().build("/book/main_activity").navigation();
        SP.put(AppConfig.Authorization, bean.getToken());
        if (isLoginOut) {
            ARouter.getInstance().build(ARoutConfig.homeActivityPath).navigation();
        }
        finish();
    }

    @Override
    public void sendSmsCodeSuccess() {
        timer = new CountDownTimer(60000, 1000) {
            @Override
            public void onTick(long l) {
//                btnCode.setText(l / 1000 + "s");
//                btnCode.setClickable(false);
            }

            @Override
            public void onFinish() {
//                btnCode.setText("重新获取");
//                btnCode.setClickable(true);
            }
        }.start();
    }

    @Override
    public void checkSmsCodeSuccess() {

    }

    @Override
    public void login() {
        if (StringUtils.isEmpty(etPhone.getText().toString().trim())) {
            ToastyUtil.showWarning("手机号不能为空");
            return;
        }
        if (!StringUtils.isPhoneNumberValid(etPhone.getText().toString().trim())) {
            ToastyUtil.showWarning("非法的手机号");
            return;
        }
        if (StringUtils.isEmpty(etPwd.getText().toString())) {
            ToastyUtil.showWarning("密码不能为空");
            return;
        }
        Loading.show(this, getString(R.string.str_loading));
        RequestBody body = RequestHelper.build()
                .add("Phone", etPhone.getText().toString())
                .add("Password", MD5Utils.getMD5(etPwd.getText().toString()))
                .create();
        mPresenter.login(body);
    }

    @Override
    public void sendSmsCode() {
        RequestBody body = RequestHelper.build()
                .add("Phone", etPhone.getText().toString())
                .add("Type", 3)
                .create();
        mPresenter.sendSmsCode(body);
    }

    @Override
    public void quickLogin() {
        RequestBody body = RequestHelper.build()
                .add("Phone", etPhone.getText().toString())
//                .add("SmsCode", etCode.getText().toString())
                .create();
        mPresenter.quickLogin(body);
    }

    @Override
    public void checkSmsCode() {
        RequestBody body = RequestHelper.build()
                .add("Phone", "")
                .add("Type", 3)
                .add("Sms", "")
                .create();
        mPresenter.checkSmsCode(body);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (timer != null) {
            timer.cancel();
        }

    }

    @Override
    public void onMainEvent(MessageEvent messageEvent) {
        super.onMainEvent(messageEvent);
        if ("registOk".equals(messageEvent.getMessage())) {
            if (isLoginOut) {
                ARouter.getInstance().build(ARoutConfig.homeActivityPath).navigation();
            }
            finish();
        }
    }
}
